/*
 * WebooksRequest.fx
 *
 * Created on 20/04/2009, 21:49:35
 */

package webooksfx.model.parser;

import java.lang.Exception;
import javafx.io.http.HttpRequest;
import webooksfx.exception.ConnectionException;
import webooksfx.exception.NoResultsException;
import webooksfx.exception.ResponseException;
import webooksfx.model.ColeccionLibros;
import webooksfx.model.Libro;
import webooksfx.model.parser.WebooksParser;
import webooksfx.model.Servicio;

/**
 * @author matias
 */

public class WebooksRequest {
    def url_base: String = "http://www.webooks.com.ar/";
    var service: String = "";
    var operation: String = "";
    var params: String = "";
    var url_request: String = "";

    var parser: WebooksParser = WebooksParser {
    };

	public var exception: Exception = null;
    public var coleccion: ColeccionLibros;
    public var libro: Libro;
    public var servicios: Servicio[];

    def request: HttpRequest = HttpRequest {
        method: HttpRequest.GET
        location: bind url_request;

        onStarted: function() {
		}
        onConnecting: function() {
        }
        onDoneConnect: function() {
        }
        onReadingHeaders: function() {
        }
        onResponseCode: function(code:Integer) {
        }
        onResponseMessage: function(msg:String) {
            if (request.responseCode != 200) {
				exception = new ResponseException();
				request.cancel();
			}
        }
        onResponseHeaders: function(headerNames: String[]) {
        }
        onToRead: function(bytes: Integer) {
        }
        onRead: function(bytes: Integer) {
		}
        onInput: function(input: java.io.InputStream) {
            try {
                parser.input_res = input;
                parser.parseResponse();
            } finally {
                input.close();
            }
        }
        onException: function(ex: Exception) {
			if (exception == null) {
				exception = new ConnectionException();
			}
        }
        onDoneRead: function() {
			if (this.operation == "search") {
				if (parser.getColeccionLibros().cant_resultados != 0) {
					coleccion = parser.getColeccionLibros();
				}
				else {
					exception = new NoResultsException();
				}
			}
			else if (this.operation == "detail") {
				libro = parser.getLibro();
			}
			else {
				servicios = parser.getServicios();
				println ("Respuesta: {servicios}");
			}
        }
        onDone: function() {
        }
    }

    function executeOperation () : Void {
		request.enqueue();
    }

    public function search (service: String, query: String, pag_actual: Integer, criterio : String) : Void {
        this.service = service;
        this.operation = "search";
        this.params = "query={query}&pag_actual={pag_actual}&criterio={criterio}";
        this.url_request = "{this.url_base}xml/{this.service}/{this.operation}?{this.params}";
		println("-------------------------------------------------------------");
		println("Search Request: {this.url_request}");
		println("-------------------------------------------------------------");
        this.executeOperation();
    }

    public function detail (service: String, id_libro: String, formato_fecha: String) : Void {
        this.service = service;
        this.operation = "detail";
        this.params = "id_libro={id_libro}&formato_fecha={formato_fecha}";
        this.url_request = "{this.url_base}xml/{this.service}/{this.operation}?{this.params}";
		println("-------------------------------------------------------------");
		println("Detail Request: {this.url_request}");
		println("-------------------------------------------------------------");
        this.executeOperation();
    }

    public function services() : Void {
        this.url_request = "{this.url_base}services.xml";
		println("-------------------------------------------------------------");
		println("Services Request: {this.url_request}");
		println("-------------------------------------------------------------");
        this.executeOperation();
    }
}